|
|
|
|
|
|
|
Using the TreeView Control |
|
|
|
|
|
|
|
|
The TreeView control is similar to a ListView in that it can display items with a combination of text and graphics. However, the TreeView does so by showing items within a tree hierarchy. If you have ever taken a math or computer science class, you already may know about the ''tree hierarchy." Figure 12.10 shows an example of a tree you might have seen before. |
|
|
|
|
|
|
|
|
Figure 12.10
A true structure defines a relationship
between objects (represented by the circles). |
|
|
|
|
|
|
|
|
Given the hierarchical nature of the TreeView control, root, parent, and child are fundamental concepts that you must understand so that you can work effectively with the control. Also, the TreeView uses the Node object extensively. Therefore, mastery of the Node object is also a requirement for effective use of the TreeView. The most common example of the TreeView is shown in the left pane of the Windows Explorer (refer to Figure 12.4). |
|
|
|
|
|
|
|
|
A hierarchy is an organization in which each part has a defined relationship with the other parts. For example, consider your family tree. Your parents are "above" you in the hierarchy, and your children are "below" you. |
|
|
|
|
|
|
|
|
In the family tree example, each person is considered to be a node on the tree. Relationships between nodes are indicated by branches. A node's branches can connect it to other nodes (that is, lines on the family tree connect you to your ancestors), althought a node does not necessarily have to have any descendants. |
|
|
|
|
|
|
|
|
Like the family tree or a real-life tree, all TreeView controls have nodes. The Nodes property of a TreeView control is itself a collection of Node objects. If you have followed the discussion up to now regarding collections, this concept probably makes some sense to you. (If you're unclear about collections, review the ListView section, earlier in this chapter.) Like the ListView's ListItems collection, the TreeView's Nodes collection has properties and methods, including an Add method used to create new nodes: |
|
|
|
|
|
|
|
|
tv1.Nodes.Add , , "mykey", "Test Node", 1, 2 |
|
|
|
|
|